package com.by.course.entity.base;

import cn.hutool.http.HttpStatus;

/**
 *
 * @author zhd
 * @date 2023/07/21 12:55
 **/
public class BaseResult<T> {

    private T data;

    private Integer code;

    private String msg;

    public T getData() {
        return data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public static final Integer OK = 0;

    private BaseResult(Builder<T> builder) {
        this.data = builder.data;
        this.code = builder.code;
        this.msg = builder.msg;
    }

    public BaseResult() {

    }

    public static Builder<String> builder() {
        return new Builder<>();
    }

    public static class Builder<T> {

        private T data;

        private Integer code = 0;

        private String msg = "success";

        public Builder<T> setData(T data) {
            this.data = data;
            return this;
        }

        public Builder<T> setMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder<T> setCode(Integer code) {
            this.code = code;
            return this;
        }

        public Builder<T> error() {
            this.code = HttpStatus.HTTP_INTERNAL_ERROR;
            this.msg = "error";
            return this;
        }

        public Builder<T> error(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
            return this;
        }

        public BaseResult<T> build() {
            return new BaseResult<>(this);
        }


    }

    public void setData(T data) {
        this.data = data;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
